package InterfazGrafica;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import principal.Lanzador;
import principal.XMLFileFilter;

public class Principal extends javax.swing.JFrame {

    /** Creates new form Principal */
    public Principal() {
        initComponents();
        cambiarIcono();
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        jbgGrupo.add(rbDOM);
        jbgGrupo.add(rbSAX);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jbgGrupo = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtArbol = new javax.swing.JTree();
        jpnTope = new javax.swing.JPanel();
        jtfRutaArchivo = new javax.swing.JTextField();
        jpnTipoParser = new javax.swing.JPanel();
        rbSAX = new javax.swing.JRadioButton();
        rbDOM = new javax.swing.JRadioButton();
        jbtIniciar = new javax.swing.JButton();
        jbtSeleccionar = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jButton5 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(675, 650));
        getContentPane().setLayout(null);

        jScrollPane1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jtArbol.setFont(new java.awt.Font("Courier New", 0, 18)); // NOI18N
        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
        jtArbol.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        jScrollPane1.setViewportView(jtArbol);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(0, 100, 480, 540);

        jpnTope.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jpnTope.setMinimumSize(new java.awt.Dimension(0, 0));
        jpnTope.setLayout(null);

        jtfRutaArchivo.setFont(new java.awt.Font("Verdana", 0, 14)); // NOI18N
        jtfRutaArchivo.setPreferredSize(new java.awt.Dimension(58, 27));
        jpnTope.add(jtfRutaArchivo);
        jtfRutaArchivo.setBounds(10, 10, 520, 30);

        jpnTipoParser.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Tipo de Parser", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Verdana", 0, 14))); // NOI18N
        jpnTipoParser.setFont(new java.awt.Font("Verdana", 0, 14));
        jpnTipoParser.setLayout(null);

        rbSAX.setFont(new java.awt.Font("Verdana", 0, 14));
        rbSAX.setSelected(true);
        rbSAX.setText("SAX ");
        jpnTipoParser.add(rbSAX);
        rbSAX.setBounds(10, 20, 60, 27);

        rbDOM.setFont(new java.awt.Font("Verdana", 0, 14));
        rbDOM.setText("DOM");
        rbDOM.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbDOMActionPerformed(evt);
            }
        });
        jpnTipoParser.add(rbDOM);
        rbDOM.setBounds(70, 20, 60, 23);

        jpnTope.add(jpnTipoParser);
        jpnTipoParser.setBounds(390, 40, 140, 50);

        jbtIniciar.setFont(new java.awt.Font("Verdana", 0, 14));
        jbtIniciar.setText("Iniciar");
        jbtIniciar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtIniciarActionPerformed(evt);
            }
        });
        jpnTope.add(jbtIniciar);
        jbtIniciar.setBounds(540, 50, 123, 30);

        jbtSeleccionar.setFont(new java.awt.Font("Verdana", 0, 14)); // NOI18N
        jbtSeleccionar.setText("Abrir Archivo");
        jbtSeleccionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtSeleccionarActionPerformed(evt);
            }
        });
        jpnTope.add(jbtSeleccionar);
        jbtSeleccionar.setBounds(540, 10, 123, 30);

        getContentPane().add(jpnTope);
        jpnTope.setBounds(0, 0, 670, 100);

        jPanel4.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel4.setLayout(null);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos"));
        jPanel3.setLayout(null);

        jButton5.setText("Agregar dato");
        jPanel3.add(jButton5);
        jButton5.setBounds(10, 30, 140, 23);

        jButton7.setText("Modificar dato");
        jPanel3.add(jButton7);
        jButton7.setBounds(10, 60, 140, 23);

        jButton8.setText("Eliminar dato");
        jPanel3.add(jButton8);
        jButton8.setBounds(10, 90, 140, 23);

        jPanel4.add(jPanel3);
        jPanel3.setBounds(10, 130, 160, 140);

        jButton2.setText("Guardar cambios");
        jPanel4.add(jButton2);
        jButton2.setBounds(30, 330, 120, 23);

        jButton9.setText("Descartar Cambios");
        jPanel4.add(jButton9);
        jButton9.setBounds(30, 300, 123, 23);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Gestión de nodos"));
        jPanel2.setLayout(null);

        jButton1.setText("Nuevo nodo");
        jPanel2.add(jButton1);
        jButton1.setBounds(10, 20, 140, 23);

        jButton4.setText("Modificar nodo");
        jPanel2.add(jButton4);
        jButton4.setBounds(10, 50, 140, 23);

        jButton3.setText("Eliminar nodo");
        jPanel2.add(jButton3);
        jButton3.setBounds(10, 80, 140, 23);

        jPanel4.add(jPanel2);
        jPanel2.setBounds(10, 10, 170, 120);

        getContentPane().add(jPanel4);
        jPanel4.setBounds(480, 100, 190, 540);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtSeleccionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtSeleccionarActionPerformed
        File fXML = null;

        if (jtfRutaArchivo.getText().trim().equals("")){
            GetFileXML("");
        }
        else{
          fXML = new File(jtfRutaArchivo.getText().trim());

          if ((fXML != null) && (fXML.exists())){
            if (fXML.isDirectory()){
                GetFileXML(fXML.getAbsolutePath().toString());
            }
            else{
            }
          }
        }

//        Lanzador lanzadorDePrograma = new Lanzador(this);
//
//        try {
//            lanzadorDePrograma.ejecutar(rbSAX.isSelected());
//
//        } catch (IOException ex) {
//            JOptionPane.showMessageDialog(this, ex.getMessage(), "Error!", JOptionPane.ERROR_MESSAGE);
//        }
    }//GEN-LAST:event_jbtSeleccionarActionPerformed

    private void jbtIniciarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtIniciarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jbtIniciarActionPerformed

    private void rbDOMActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbDOMActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_rbDOMActionPerformed

    public static void main(String args[]) {
        try{
            javax.swing.UIManager.setLookAndFeel(
            UIManager.getSystemLookAndFeelClassName());
        }catch (Exception e){}
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Principal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.ButtonGroup jbgGrupo;
    private javax.swing.JButton jbtIniciar;
    private javax.swing.JButton jbtSeleccionar;
    private javax.swing.JPanel jpnTipoParser;
    private javax.swing.JPanel jpnTope;
    private javax.swing.JTree jtArbol;
    private javax.swing.JTextField jtfRutaArchivo;
    private javax.swing.JRadioButton rbDOM;
    private javax.swing.JRadioButton rbSAX;
    // End of variables declaration//GEN-END:variables
    private DefaultMutableTreeNode nodoAnterior = null;
    private DefaultMutableTreeNode nodoPadre = null;
    private DefaultTreeModel modelo;
    private tipoTAG ultimoTipoTag;
    private List nodosPadre = new ArrayList<DefaultMutableTreeNode>();

    public enum tipoTAG{INICIO, FIN, AUTOCONTENIDO, CONTENIDO, PROLOGO};

    private void cambiarIcono() {
        DefaultTreeCellRenderer render= (DefaultTreeCellRenderer)jtArbol.getCellRenderer();
        render.setLeafIcon(null);
        render.setOpenIcon(null);
        render.setClosedIcon(null);
    }

    public void cargarRaiz(String stNombre){
        DefaultMutableTreeNode nodo = new DefaultMutableTreeNode(stNombre);
        modelo = new DefaultTreeModel(nodo);
        jtArbol.setModel(modelo);
        nodoAnterior = nodo;
        nodosPadre.add(nodo);
    }

    public void agregarTag(String stNombre, tipoTAG ttTAG){
        DefaultMutableTreeNode nodo = new DefaultMutableTreeNode(stNombre);
        switch (ttTAG){
            case PROLOGO: {
                modelo.insertNodeInto(nodo, nodoAnterior, nodoAnterior.getChildCount());
                break;
            }
            case INICIO: {
                if (ultimoTipoTag == tipoTAG.INICIO) {
                    nodosPadre.add(nodo);
                    modelo.insertNodeInto(nodo, nodoAnterior, nodoAnterior.getChildCount());
                }
                else if ((ultimoTipoTag == tipoTAG.FIN) || (ultimoTipoTag == tipoTAG.PROLOGO)) {
                    nodoPadre = (DefaultMutableTreeNode) nodosPadre.get(nodosPadre.size() - 1);
                    modelo.insertNodeInto(nodo, nodoPadre, nodoPadre.getChildCount());
                    nodosPadre.add(nodo);
                }
                break;
            }
            case FIN:{
                    nodoPadre = (DefaultMutableTreeNode) nodosPadre.get(nodosPadre.size() - 1);
                    nodosPadre.remove(nodoPadre);
                    nodoPadre = (DefaultMutableTreeNode) nodosPadre.get(nodosPadre.size() - 1);
                    modelo.insertNodeInto(nodo, nodoPadre, nodoPadre.getChildCount());
                break;
            }}
        nodoAnterior = nodo;
        ultimoTipoTag = ttTAG;
    }

    private File GetFileXML(String pRuta){
        File archivo = null;
        JFileChooser fileChooser = new JFileChooser();
        XMLFileFilter filtro = new XMLFileFilter();
        // Inicializaciòn del cuadro de dialogo
        fileChooser.setDialogTitle("Seleccione archivo xml para analizar");
        fileChooser.setFileFilter(filtro);

        if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
           jtfRutaArchivo.setText(fileChooser.getSelectedFile().getPath());
        }

        return (archivo);
    }
}
